#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define KEY 6
char *keyword[KEY] = {"for", "if", "then", "else", "while", "do"};
char id[1024];
int num;
int token;

void lexical(FILE *fp) {
	char x = getc(fp);
	int i;
	int j;

	num = 0;
	//strcpy(id, "\0");
    memset(id,'\0',2014);
        if((x>='a'&&x<='z')||(x>='A'&&x<='Z'))                   //id
	{
	  
	    int count=0;
	    int test;   
	    while((x>='0'&&x<='9')||(x>='a'&&x<='z')||(x>='A'&&x<='Z'))
	    {
	        id[count++]=x;

	        x=getc(fp);
	    }
           
            token=10;
	    for(i=0;i<6;i++)//keyword
	    {
	        test=0;
	       for(j=0;j<count;j++)
	       {
	          if(id[j]==keyword[i][j])
		   test++; 
	       }
	       
	       if(test==count)
	       {
	         
	          token=i+1;
		  break;
	       }
	       
	    }
	    
	    ungetc(x,fp);

	    //memset(id,'\0',2014);
            
	}

	else if(x>='0'&&x<='9')      //numer
	{
	   int sum=0;
	   
	 
	   while(x>='0'&&x<='9')
	   {
	       sum=sum*10+(x-'0');
	      
	       x=getc(fp);
	   }
	   ungetc(x,fp);
	   num=sum;
	   token=11;
	    
	}
        
	else
	{
	    int count=0;
	    if(x=='+')
	    {
	        memset(id,'\0',2014);
	        token=13;
		id[count++]= x;
		
	    }

	    else if(x=='-')
	    {
	      token=14;
	      id[count++]=x;
	      

	    }
	    
	    else if(x=='*')
	    {
	       token=15;
	       id[count++]= x;
	      
	    }
 
            else if(x=='/')
	    {
	       token=16;
	       id[count++]=x;
	       
	    }
	    
	
	    else if(x==':')
	    {
	       id[count++]=x;
	      
	       x=getc(fp);
	       if(x =='=')
	       {
	          token=18;
		  id[count++]=x;
	       }
	 
	       else
	       {
	          token=17;
		  ungetc(x,fp);
		 
	       }
	       
               
               
	    }

	    else if(x=='<')
	    {
               id[count++]=x;
	      
	       x=getc(fp);
	       if(x=='>')
	       {
	          token=21;
		  id[count++]=x;
	       }
	       
	       else if(x=='=')
	       {
                  token=22;
		  id[count++]= x;
	       }
	   
	       else
	       {
	          token=20;
		  ungetc(x,fp);
	       }
	       
	    }
	    else if(x=='>')
	    {
	       id[count++]=x;
	       x=getc(fp);
	       if(x=='=')
	       {
	          token=24;
		  id[count++]=x;
	       }
	      else
	    {  token=23;
		   ungetc(x,fp);
		}
                
	    }
        else if(x=='=')
	    {
	        id[count++]= x;
	        token=25;
		
	    }
	    else if(x==';')
	    {
	       id[count++]=x;
	       token=26;
	       
	    }
	    else if(x=='(')
	    {
	       id[count++]=x;
	       token=27;
	       
	    }
        else if(x==')')
	    {
	       id[count++]=x;
	       token=28;
	       
	    }
	    else if(x=='#')
	    {
	       id[count++]=x;
	       token=0;
	       
	    }
	    else if(x==' ')
	    {
	    	id[count++]=x;
	    	x=getc(fp);
	    	if(x==' ')
	    	{
	    		token=-2;
			  id[count++]=x;
	    	}
	    	else
			{
				token=-2;
			  ungetc(x,fp);
	    	}
	    	
	    }

            else
	    {
	        token=-1; 
	    }
	     
     }


	//process x
	//process each regex here
	//store the result in global variables id, num, or token
	//in case of backtrack, use
	//ungetc(x, fp);
	
}

int main(int argc, const char *argv[])
{
	if(argc < 2) {
		printf("insufficient args\n");
		return 1;
	}
	FILE *fp = fopen(argv[1], "r");
	do {
		lexical(fp);
		switch(token) {
			case 10: printf("(id, %s)\n", id); break;
			case 11: printf("(num, %d)\n", num); break;
			case -1: printf("error!\n"); break;
			case -2: printf("");break;
			default: printf("(%d, %s)\n", token, id); 		
		}
	} while(token != 0);
	

	fclose(fp);
	return 0;
}
